package com.wpp.rpc.client;

import com.wpp.rpc.api.ComplexService;
import com.wpp.rpc.api.HelloService;
import com.wpp.rpc.model.Message;
import com.wpp.rpc.model.MessageList;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RpcClient {
    public static void main(String[] args) throws InterruptedException {
        new RpcClient().start();
    }

    private void start() throws InterruptedException {
        try {
            Registry localhost = LocateRegistry.getRegistry("localhost", 8888);


            HelloService helloService = (HelloService) localhost.lookup("hello");

            ComplexService complexService = (ComplexService) localhost.lookup("complex");

            for (int i = 0; i < 10; i++) {
                String msg = helloService.hello("msg" + i);
//                Thread.sleep(1000);
                System.out.println(msg);
            }
            for (int i = 0; i < 10; i++) {
                MessageList list = complexService.addMessage(new Message(i, "complex msg" + i));
                System.out.println(list.getList().size());
            }

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }

    }
}
